﻿using Direct.IDAL.User.Money;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.User.Money
{
    /// <summary>
    /// 电子币操作类
    /// </summary>
    public class VirtualMoneyRepository:AbsRepository, IVirtualMoneyRepository
    {

        #region 申请电子币
        /// <summary>
        /// 申请电子币
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool Apply(VirtualMoneyReq req)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO VirtualMoneyApply ");
            sb.Append(" ([UserCode] ");
            sb.Append(" ,[UserName] ");
            sb.Append(" ,[Amount] ");
            sb.Append(" ,[ApplyTime] ");
            sb.Append(" ,[Remark] ");
            sb.Append(" ,[State]) ");
            sb.Append(" VALUES ");
            sb.AppendFormat("('{0}'",req.UserCode);
            sb.AppendFormat(",'{0}'", req.UserName);
            sb.AppendFormat(",'{0}'", req.Amount);
            sb.AppendFormat(",'{0}'", req.ApplyTime);
            sb.AppendFormat(",'{0}'", req.Remark);
            sb.AppendFormat(",'{0}')", req.State);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 查询申请记录
        /// <summary>
        /// 查询申请记录
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<PagedList<VirtualMoney>> Record(string usercode,int pageindex,int pagesize)
        {
            PageParam model = new PageParam()
            {
                OrderBy = "Id DESC",
                PageIndex = pageindex,
                PageSize = pagesize,
                StrWhere = $"UserCode='{usercode}'",
                TableName = "VirtualMoneyApply"
            };
            return await base.PageQuery<VirtualMoney>(model);
            //StringBuilder sb = new StringBuilder();
            //sb.Append(" SELECT * FROM VirtualMoneyApply ");
            //sb.AppendFormat(" WHERE UserCode='{0}' ", usercode);
            //var result = await base.QueryAsync<VirtualMoney>(sb.ToString());
            //return result as IList<VirtualMoney>;
        }
        #endregion

        #region 查电子币余额
        /// <summary>
        /// 查询电子币余额
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public decimal QueryVirtualMoney(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT VirtualMoney FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserCode='{0}' ", usercode);
            return base.QueryFirstT<decimal>(sb.ToString());
        }
        #endregion

    }
}
